﻿/// <reference path="vswd-ext_2.2.js" />



//*********************Create GPRS Authration Window*****************
function GPRSAuthWindow(sid, devicename, data,isphotoauth) {

    var _devicename = devicename;
    var _data = data ;
    var _sid = sid;
    var _isphotoauth = isphotoauth;

    var chkColumnAuth = new Ext.grid.CheckColumn({
        header: '',
        width: 25,
        sortable: false,
        dataIndex: 'choosed'
    });

    var colAuthColumnModel = new Ext.grid.ColumnModel({
        columns: [
         { header: _TrackerMain_EventCode, width: 200, dataIndex: 'val' },
         chkColumnAuth
    ],
        defaults: {
            sortable: true,
            menuDisabled: true,
            width: 75
        }
    });


    function getAuth() {
        var strBLow = padLeft('', '0', 32);
        var strBHigh = padLeft('', '0', 32);
        var index;
        sto.each(function(record) {
            index = parseInt(record.get("valueid"));
            var checked = record.get("choosed");
            if (checked) {
                if (index <= 32) 
                    strBLow = replacechr(strBLow, 32 - index, "1");
                else
                    strBHigh = replacechr(strBHigh, 32 - (index - 32), "1");
            }

        });       
        var strdefault;
        //give default value for event 34, 35
        if (_isphotoauth)
            strdefault = "0";
        else
            strdefault = "1";
        index = 34;
        strBHigh = replacechr(strBHigh, 32 - (index - 32), strdefault);
        index = 35;
        strBHigh = replacechr(strBHigh, 32 - (index - 32), strdefault);
        var strHex = padLeft(parseInt(strBHigh, 2).toString(16), '0', 8) + padLeft(parseInt(strBLow, 2).toString(16), '0', 8);
        strHex = strHex.toUpperCase();
        return strHex;
    }

    function sendAuthCommand() {
        var mt88cmd;
        if (_isphotoauth)
            mt88cmd = EnumMT88Command.Set_Event_Flag_for_Taking_Picture;
        else
            mt88cmd = EnumMT88Command.Set_Event_Flag_for_GPRS;
        var mt88param = getAuth();
        var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
        var gvvtparam = "";
        if (_sid != null && _sid != "")
            addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
        else
            addTCPCommand(gvvtcommand, gvvtparam, mt88cmd, mt88param);
    }
    
    var btnsend = new Ext.Button({
        text: _TrackerMain_Send,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sendAuthCommand();
        }
    });

    var sto;
    if (_isphotoauth) {
        sto = createEventCodeStore(EnumAuthType.Photo, false);
    }
    else {
        sto = createEventCodeStore(EnumAuthType.GPRS, false);
    }
    
    var grd = new Ext.grid.GridPanel({
        store: sto,
        colModel: colAuthColumnModel,
        plugins:[chkColumnAuth],
        //width: 885,
        //height: 400,
        
        buttons:[btnsend]
    });

    var win = new Ext.Window({
        title: _ConfigResource_GPRSConfig,
        layout: 'fit',
        height: 470,
        width: 300,
        items: grd,
        constrain:true,
        x: nextwindowx, //defined in paneldevicelist.js
        y: nextwindowy

    });

    if (_isphotoauth) {
        win.setTitle(_Photo_PhotoAuth+'('+ _devicename +')');
    }
    else
        win.setTitle(_ConfigResource_GPRSConfig+'(' + _devicename + ')');
    win.show();
    nextwindowx += 20;
    nextwindowy += 20;

    win.addListener('close', function(panel) {
        if (_isphotoauth)
            photosetauthwindow = null;
        else
            gprssetauthwindow = null;
    });

    grd.addListener('viewready', function(obj) {
        var high8bytes = _data.substr(0, 8);
        var highB = padLeft(parseInt(high8bytes, 16).toString(2), '0', 32);
        var low8bytes = _data.substr(8, 8);
        var lowB = padLeft(parseInt(low8bytes, 16).toString(2), '0', 32);
        var i;
        for (i = 0; i < 32; i++) {
            var checked = lowB.substr(32 - i - 1, 1) == "1" ? true : false;
            var rowindex = sto.find("valueid", (i + 1).toString());
            if (rowindex >= 0) {
                var record = sto.getAt(rowindex);
                record.set("choosed", checked);
            }
        }

        for (i = 0; i < 32; i++) {
            var checked = highB.substr(32 - i - 1, 1) == "1" ? true : false;
            var rowindex = sto.find("valueid", (32 + (i + 1)).toString());
            if (rowindex >= 0) {
                var record = sto.getAt(rowindex);
                record.set("choosed", checked);
            }
        }

    });

    grd.setTitle(_devicename);
    
    this.beginSendAuthCommand = function() {
        sendAuthCommand();
    }

    this.show = function() {
        if(!win.isVisible())
            win.show();
    }
    
    this.getAuth = function() {
        return getAuth();
    }
}

//*********************Create SMS Authration Window*****************
function SMSAuthWindow() {
    var chkColumnAuth = new Ext.grid.CheckColumn({
        header: '',
        width: 25,
        sortable: false,
        dataIndex: 'choosed'
    });
    
    var colAuthColumnModel = new Ext.grid.ColumnModel({
        columns: [
         { header: _TrackerMain_EventCode, width: 200, dataIndex: 'val' },
         chkColumnAuth
    ],
        defaults: {
            sortable: true,
            menuDisabled: true,
            width: 75
        }
    });
    
    var _devicename;
    var _data;
    var _sid;
    var _phonenumber;
    var _index;
    
    var sto = createEventCodeStore(EnumAuthType.SMS, false);

    function getAuth() {
        var s = "";
        sto.each(function(record) {
            index = parseInt(record.get("valueid"));
            var checked = record.get("choosed");
            if (checked) {
                if (s.length == 0)
                    s = index.toString();
                else
                    s += "," + index.toString();
            }

        });
        return s;
    }
    
    var btnsend = new Ext.Button({
        text: _TrackerMain_Send,
        style: 'margin-right:16px',
        width: 80,
        handler: function() {
            sendAuthCommand();
        }
    });

    function sendAuthCommand() {
        var mt88cmd = EnumMT88Command.Set_Authorize_phone_number_and_SMS_Event_Flag;
        var mt88param = _index + ',' + _phonenumber + ',' + getAuth();
        var gvvtcommand = EnumGPRSCommand.NotSupportThisCommand;
        var gvvtparam = "";
        if (_sid != null && _sid != "")
            addOneTCPCommand(_sid, gvvtcommand, gvvtparam, mt88cmd, mt88param);
        else
            addTCPCommand(gvvtcommand, gvvtparam, mt88cmd, mt88param);
    }
    
    var grd = new Ext.grid.GridPanel({
        store: sto,
        colModel: colAuthColumnModel,
        plugins: [chkColumnAuth],
        width: 340,
        height: 400,
        title: _ConfigResource_SMSConfig,
        buttons:[btnsend]
    });

    var win = new Ext.Window({
        title: _ConfigResource_SMSConfig,
        layout: 'fit',
        height: 470,
        width: 360,
        items: grd,
        constrain:true,
        x: nextwindowx, //defined in paneldevicelist.js
        y: nextwindowy

    });

    win.show();
    nextwindowx += 20;
    nextwindowy += 20;

    grd.addListener('viewready', function(obj) {
        grd.setTitle(_devicename);
        var tmp = _data.split(',');
        if (tmp.length >= 3) {
            _index = tmp[0];
            _phonenumber = tmp[1];
            grd.getColumnModel().setColumnHeader(1, tmp[1]);
            grd.getColumnModel().setColumnWidth(1, 150);

            var first8bytes = tmp[2].substr(0, 8);
            var second8bytes = tmp[2].substr(8, 8);
            var third8bytes = tmp[2].substr(16, 8);

            var firstB = padLeft(parseInt(first8bytes, 16).toString(2), '0', 32);
            var secondB = padLeft(parseInt(second8bytes, 16).toString(2), '0', 32);
            var thirdB = padLeft(parseInt(third8bytes, 16).toString(2), '0', 32);
            var i;
            for (i = 0; i < 32; i++) {
                var checked = secondB.substr(32 - i - 1, 1) == "1" ? true : false;
                var rowindex = sto.find("valueid", (i + 1).toString());
                if (rowindex >= 0) {
                    var record = sto.getAt(rowindex);
                    record.set("choosed", checked);
                }
            }

            for (i = 0; i < 32; i++) {
                var checked = firstB.substr(32 - i - 1, 1) == "1" ? true : false;
                var rowindex = sto.find("valueid", (32 + (i + 1)).toString());
                if (rowindex >= 0) {
                    var record = sto.getAt(rowindex);
                    record.set("choosed", checked);
                }
            }


            for (i = 0; i < 32; i++) {
                var checked = thirdB.substr(32 - i - 1, 1) == "1" ? true : false;
                var rowindex = sto.find("valueid", (64 + (i + 1)).toString());
                if (rowindex >= 0) {
                    var record = sto.getAt(rowindex);
                    record.set("choosed", checked);
                }
            }
        }

    });

    this.beginSetAuth = function(sid, devicename, data, phonenumber) {
        _sid = sid;
        _devicename = devicename;
        _data = data;
        _phonenumber = phonenumber;
    };

    this.show = function() {
        if (!win.isVisible())
            win.show();
    };

    

}